import { defineStore } from "pinia";

interface Person {
  id: number;
  name: string;
}

type Persons = Person[];

interface CounterState {
  count: number;
  isShow: boolean;
  persons: Persons;
}

export const useCounterStore = defineStore("counter", {
  // state是一个函数
  // 函数必须返回一个对象
  state: (): CounterState => {
    return {
      count: 0,
      isShow: true,
      // 一旦数据中包含对象或数组，就需要定义类型
      persons: [
        // {id: 1, name: 'jack'}
      ],
    };
  },
  getters: {
    oddOrEven(state) {
      return (state.count & 1) === 1 ? "奇数" : "偶数";
    },
  },
  actions: {
    // action接受n个参数
    increment(num: number, ...args: number[]) {
      console.log(args);
      // 直接更新数据
      this.count += num;
    },
    decrement(num: number) {
      this.count -= num;
    },
  },
  // 没有mutations
});
